﻿// CParamSetDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "MesTest.h"
#include "CParamSetDlg.h"
#include "afxdialogex.h"
#include "MesTestDlg.h"


// CParamSetDlg 对话框

IMPLEMENT_DYNAMIC(CParamSetDlg, CDialogEx)

CParamSetDlg::CParamSetDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG1, pParent)
	, m_CHK_bServer(FALSE)
	, m_CHK_bShiledHeartBeat(FALSE)
{
	
}

CParamSetDlg::~CParamSetDlg()
{
}

void CParamSetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COM_PRODUCTTYPER, m_ProdectTypeR);
	DDX_Check(pDX, IDC_CHECK_bServer, m_CHK_bServer);
	DDX_Check(pDX, IDC_CHECK_ShiledHeartBeat, m_CHK_bShiledHeartBeat);
}


BEGIN_MESSAGE_MAP(CParamSetDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CParamSetDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BTN_factoryNo, &CParamSetDlg::OnBnClickedBtnfactoryNo)
	ON_BN_CLICKED(IDC_BTN_machineNo, &CParamSetDlg::OnBnClickedBtnmachineNo)
	ON_BN_CLICKED(IDC_BTN_UpperComputerUser, &CParamSetDlg::OnBnClickedBtnUpperComputerUser)
	ON_BN_CLICKED(IDC_BTN_PRODUCTTYPE, &CParamSetDlg::OnBnClickedBtnProducttype)
	ON_BN_CLICKED(IDC_BTN_PRODUCTIDEN, &CParamSetDlg::OnBnClickedBtnProductiden)
	ON_BN_CLICKED(IDC_BTN_DANJIACODE, &CParamSetDlg::OnBnClickedBtnDanjiacode)
	ON_BN_CLICKED(IDC_BTN_IP, &CParamSetDlg::OnBnClickedBtnIp)
	ON_BN_CLICKED(IDC_BTN_PORT, &CParamSetDlg::OnBnClickedBtnPort)
	ON_BN_CLICKED(IDC_BTN_MESOUTLOG, &CParamSetDlg::OnBnClickedBtnMesoutlog)
	ON_BN_CLICKED(IDC_BTN_MESLOAD, &CParamSetDlg::OnBnClickedBtnMesload)
	ON_BN_CLICKED(IDC_BTN_TIMEOUT, &CParamSetDlg::OnBnClickedBtnTimeout)
	ON_EN_CHANGE(IDC_EDT_MESACCOUNT, &CParamSetDlg::OnEnChangeEdtMesaccount)
	ON_EN_CHANGE(IDC_EDT_MESPASS, &CParamSetDlg::OnEnChangeEdtMespass)
	ON_BN_CLICKED(IDC_BTN_HeartBeatSpanTime, &CParamSetDlg::OnBnClickedBtnHeartbeatspantime)
	ON_BN_CLICKED(IDC_CHECK_bServer, &CParamSetDlg::OnBnClickedCheckbserver)
	ON_BN_CLICKED(IDC_CHECK_ShiledHeartBeat, &CParamSetDlg::OnBnClickedCheckShiledheartbeat)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDCANCEL, &CParamSetDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON_TIMEJIANGE, &CParamSetDlg::OnBnClickedButtonTimejiange)
	ON_BN_CLICKED(IDC_BUTTON_EQUIMENT, &CParamSetDlg::OnBnClickedButtonEquiment)
END_MESSAGE_MAP()


// CParamSetDlg 消息处理程序


void CParamSetDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	KillTimer(UM_TIMER_TIME);
	CDialogEx::OnOK();
}

BOOL CParamSetDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	InitParam();
	SetTimer(UM_TIMER_TIME, 1000, NULL);
	return TRUE;
}

void CParamSetDlg::InitParam()
{
	UpdateData(TRUE);
	
	SetDlgItemText(IDC_EDT_factoryNo, g_InfoMes.factoryNo);

	SetDlgItemText(IDC_EDT_machineNo, g_InfoMes.machineNo);

	//IP
	SetDlgItemText(IDC_EDT_IP, g_InfoMes.IP);

	//端口
	CString cstrItem;
	cstrItem.Format("%d", g_InfoMes.Port);
	SetDlgItemText(IDC_EDT_PORT, cstrItem);
	

	//超时时间
	CString strTimeOut;
	strTimeOut.Format("%.2f", g_InfoMes.dTimeOut);
	SetDlgItemText(IDC_EDT_TIMEOUT, strTimeOut);

	//使用者名称
	SetDlgItemText(IDC_EDT_UpperComputerUser, g_InfoMes.EventUser);

	SetDlgItemText(IDC_EDT_MESACCOUNT, g_InfoMes.MesUserName);
	SetDlgItemText(IDC_EDT_MESPASS, g_InfoMes.MesUserPassWord);

	//心跳间隔时间
	CString strHeartBeatSpanTime;
	strHeartBeatSpanTime.Format("%d", g_InfoMes.nHeartBeatSpanTime);
	SetDlgItemText(IDC_EDIT_HeartBeatSpanTime, strHeartBeatSpanTime);

	//卷号
	SetDlgItemText(IDC_EDT_BATCH, g_InfoMes.cstrBatch);

	//实时上传间隔
	CString strTime;
	strTime.Format("%d", g_InfoMes.nTimeJianGe);
	SetDlgItemText(IDC_EDIT_TIMEJIANGE, strTime);

	//服务器或客户端
	m_CHK_bServer = g_InfoMes.bServer;

	SetDlgItemText(IDC_EDIT_EQUIMENTNO, g_InfoMes.EquimentNo);
	//屏蔽心跳
	m_CHK_bShiledHeartBeat = g_InfoMes.bShiledHeartBeat;

	cstrItem.Format(_T("正极"));  //返修产品
	m_ProdectTypeR.AddString(cstrItem);
	cstrItem.Format(_T("负极"));//试制产品
	m_ProdectTypeR.AddString(cstrItem);
	m_ProdectTypeR.SetCurSel(g_InfoMes.nProdectType);

	if (g_Land.PassLevel != 3)
	{
		GetDlgItem(IDC_BTN_factoryNo)->EnableWindow(0);
		GetDlgItem(IDC_BTN_machineNo)->EnableWindow(0);
		GetDlgItem(IDC_BTN_UpperComputerUser)->EnableWindow(0);
		GetDlgItem(IDC_BTN_PRODUCTTYPE)->EnableWindow(0);
		GetDlgItem(IDC_BTN_IP)->EnableWindow(0);
		GetDlgItem(IDC_BTN_PORT)->EnableWindow(0);
		GetDlgItem(IDC_BTN_TIMEOUT)->EnableWindow(0);
		GetDlgItem(IDC_BTN_HeartBeatSpanTime)->EnableWindow(0);
		GetDlgItem(IDC_BTN_TIMEOUT)->EnableWindow(0);
		GetDlgItem(IDC_CHECK_bServer)->EnableWindow(0);
		GetDlgItem(IDC_CHECK_ShiledHeartBeat)->EnableWindow(0);
		GetDlgItem(IDC_BTN_PRODUCTIDEN)->EnableWindow(0);
		GetDlgItem(IDC_EDIT_EQUIMENTNO)->EnableWindow(0);
	}

	UpdateData(FALSE);
}

void CParamSetDlg::OnBnClickedBtnfactoryNo()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDT_factoryNo, g_InfoMes.factoryNo);
	WritePrivateProfileString("EquipmentInfo", "factoryNo", g_InfoMes.factoryNo, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedBtnmachineNo()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDT_machineNo, g_InfoMes.machineNo);
	WritePrivateProfileString("EquipmentInfo", "machineNo", g_InfoMes.machineNo, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedBtnUpperComputerUser()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDT_UpperComputerUser, g_InfoMes.EventUser);
	WritePrivateProfileString("EquipmentInfo", "EventUser", g_InfoMes.EventUser, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedBtnProducttype()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	int Type = 0;
	g_InfoMes.nProdectType=m_ProdectTypeR.GetCurSel();
	
	WritePrivateProfileInt("EquipmentInfo", "ProdectType", g_InfoMes.nProdectType, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedBtnProductiden()
{
	// TODO: 在此添加控件通知处理程序代码

}


void CParamSetDlg::OnBnClickedBtnDanjiacode()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CParamSetDlg::OnBnClickedBtnIp()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDT_IP, g_InfoMes.IP);
	WritePrivateProfileString("Server", "IP", g_InfoMes.IP, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedBtnPort()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	CString strPort;
	GetDlgItemText(IDC_EDT_PORT, strPort);
	g_InfoMes.Port = atoi(strPort);
	WritePrivateProfileInt("Server", "Port", g_InfoMes.Port, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedBtnMesoutlog()
{
	// TODO: 在此添加控件通知处理程序代码

	SetDlgItemText(IDC_EDT_MESACCOUNT, "");
	SetDlgItemText(IDC_EDT_ACCOUNT, "");
	g_InfoMes.MesUserName = "";
	g_InfoMes.MesUserPassWord = "";
	
}


void CParamSetDlg::OnBnClickedBtnMesload()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDT_MESACCOUNT, g_InfoMes.MesUserName);
	GetDlgItemText(IDC_EDT_MESPASS, g_InfoMes.MesUserPassWord);
	
	std::string  strSendData;

	((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.ValidateLogonReport(strSendData,1);
	HTuple  hvSendData(strSendData.c_str());

	if (g_bUseMes)
	{
		g_SendData.Append(hvSendData);

		CString cstrLog;
		cstrLog.Format("MES账户登录,内容:%s", strSendData.c_str());
		((CMesTestDlg*)g_pMainDialog)->WriteOperateLog(cstrLog);
		((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
		//((CMesTestDlg*)g_pMainDialog)->m_tMesSocketTcp.WriteStringToMES(hvSendData);
	}
	else
	{
		CString cstrLog;
		cstrLog.Format("MES账户登录,MES通讯关闭");
		((CMesTestDlg*)g_pMainDialog)->WriteOperateLog(cstrLog);
		((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
	}

	
	
}


void CParamSetDlg::OnBnClickedBtnTimeout()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	CString strTimeOut;
	GetDlgItemText(IDC_EDT_TIMEOUT, strTimeOut);
	g_InfoMes.dTimeOut = atof(strTimeOut);
	WritePrivateProfileDouble("EquipmentInfo", "dTimeOut", g_InfoMes.dTimeOut, g_FilePathLog);
}


void CParamSetDlg::OnEnChangeEdtMesaccount()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	//账户
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDT_MESACCOUNT, g_InfoMes.MesUserName);
}


void CParamSetDlg::OnEnChangeEdtMespass()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	//密码
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDT_MESPASS, g_InfoMes.MesUserPassWord);
}


void CParamSetDlg::OnBnClickedBtnHeartbeatspantime()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	//心跳间隔时间
	CString strHeartBeatSpanTime;
	GetDlgItemText(IDC_EDIT_HeartBeatSpanTime, strHeartBeatSpanTime);
	g_InfoMes.nHeartBeatSpanTime = atoi(strHeartBeatSpanTime);
	if (g_InfoMes.nHeartBeatSpanTime == 0)
	{
		g_InfoMes.nHeartBeatSpanTime = 1;
	}
	WritePrivateProfileInt("EquipmentInfo", "nHeartBeatSpanTime", g_InfoMes.nHeartBeatSpanTime, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedCheckbserver()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	g_InfoMes.bServer= m_CHK_bServer;
}


void CParamSetDlg::OnBnClickedCheckShiledheartbeat()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	g_InfoMes.bShiledHeartBeat= m_CHK_bShiledHeartBeat;
}


void CParamSetDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (nIDEvent == UM_TIMER_TIME)
	{
		if (1==g_InfoMes.bSuccess)
		{
			g_InfoMes.bSuccess = -1;
			AfxMessageBox("MES登录成功!");
		}
		else if(0==g_InfoMes.bSuccess)
		{
			g_InfoMes.bSuccess = -1;
			AfxMessageBox("MES登录失败!");
		}
	}
	CDialogEx::OnTimer(nIDEvent);
}


void CParamSetDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}


void CParamSetDlg::OnBnClickedButtonTimejiange()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	//心跳间隔时间
	CString strHeartBeatSpanTime;

	GetDlgItemText(IDC_EDIT_TIMEJIANGE, strHeartBeatSpanTime);
	g_InfoMes.nTimeJianGe = atoi(strHeartBeatSpanTime);	
	if (g_InfoMes.nTimeJianGe <= 1)
	{
		g_InfoMes.nTimeJianGe = 1;
	}
	WritePrivateProfileInt("EquipmentInfo", "nTimeJianGe", g_InfoMes.nTimeJianGe, g_FilePathLog);
}


void CParamSetDlg::OnBnClickedButtonEquiment()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	GetDlgItemText(IDC_EDIT_EQUIMENTNO, g_InfoMes.EquimentNo);
	WritePrivateProfileString("EquipmentInfo", "EquimentNo", g_InfoMes.EquimentNo, g_FilePathLog);

}
